using System;
using System.Xml;
using TaskLib;
using Guadagno.Utilities;

namespace TaskServiceHarness
{
    /// <summary>
    /// Console based test application for the TaskScheduler
    /// </summary>
    class Harness
    {

        #region Constructors

        public Harness()
        {
            loadConfiguration();
        }

        #endregion Constructors

        #region Private Variables

        private TaskServiceHarnessConfig _config;
        private TaskPoller _taskPoller;

        // Other Variables
        private readonly log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Private Constants

        #endregion Private Constants

        #region Private Enums

        #endregion Private Enums

        #endregion Private Variables

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            
            int ch;

            Harness harness = new Harness();
            ConsoleSetup.SetConsoleTitle(Properties.Resources.Service_Description);
            ConsoleSetup.SetConsoleInputMode(ConsoleSetup.ConsoleInputMode.Raw);

            harness.OnStart(null);

            while ((ch = Console.In.Read()) != -1)
            {
                if (Char.ToUpper((char)ch) == 'Q')
                {
                    Console.WriteLine(Properties.Resources.Console_StopRequestRecieved);
                    break;
                }
                if (ch == 13)
                {
                    Console.WriteLine();
                }
                else
                {
                    Console.Write((char)ch);
                }
            }

            harness.OnStop();
        }

        #region Methods

        #region OnStart
        /// <summary>
        /// Set things in motion so your service can do its work.
        /// </summary>
        protected void OnStart(string[] args)
        {
            
            _log.Debug(Properties.Resources.ServiceStarting);
            _log.DebugFormat(Properties.Resources.PluginPath, _config.PluginPath);
            _taskPoller = new TaskPoller(_config.PluginPath);
            _taskPoller.Start();
            
            _log.Debug(Properties.Resources.ServiceStarted);
            _log.Debug(Properties.Resources.Console_PressQ);
            
        }

        #endregion OnStart

        #region OnStop

        /// <summary>
        /// Stop this service.
        /// </summary>
        protected void OnStop()
        {
            _log.Debug(Properties.Resources.ServiceStopping);
            _taskPoller.Shutdown();
            _log.Debug(Properties.Resources.ServiceStopped);
        }

        #endregion OnStop

        #endregion Methods

        #region Helper Functions

        #region loadConfiguration
        private void loadConfiguration()
        {
            string configFile = System.Reflection.Assembly.GetExecutingAssembly().Location + ".Settings.xml";
            _config = new TaskServiceHarnessConfig(null, configFile);

            if (!string.IsNullOrEmpty(_config.Log4NetConfig))
            {
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(_config.Log4NetConfig);
                log4net.Config.XmlConfigurator.Configure(xml.DocumentElement);
            }

            _log.Debug(Properties.Resources.ConfigurationLoaded);
        }
        #endregion loadConfiguration

        #endregion Helper Functions
    }
}